package my;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class TwoSum {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TwoSum ts = new TwoSum();
		ts.add(2);
		System.out.println(ts.find(2));
		ts.add(1);
		System.out.println(ts.find(3));
		ts.add(4);
		System.out.println(ts.find(5));
	}
	HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

	public void add(int number) {
		if (map.containsKey(number)) {
			map.put(number, map.get(number) + 1);
		} else {
			map.put(number, 1);
		}
	}

	@SuppressWarnings("rawtypes")
	public boolean find(int value) {
		Iterator it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Entry) it.next();
			int key = (Integer) entry.getKey();
			if (map.containsKey(value - key)) {
				if (value == 2 * key) {
					return map.get(key) >= 2;
				}
				return true;
			}
		}
		return false;
	}
}
